// Copyright 2023 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.mediapipe.tasks.vision.imagesegmenter;

import com.google.auto.value.AutoValue;
import com.google.mediapipe.framework.image.MPImage;
import com.google.mediapipe.tasks.core.TaskResult;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/** Represents the segmentation results generated by {@link ImageSegmenter}. */
@AutoValue
public abstract class ImageSegmenterResult implements TaskResult {

  /**
   * Creates an {@link ImageSegmenterResult} instance from a list of segmentation MPImage.
   *
   * @param confidenceMasks an {@link Optional} of {@link List} of MPImage in IMAGE_FORMAT_VEC32F1
   *     format representing the confidence masks, where, for each mask, each pixel represents the
   *     prediction confidence, usually in the [0, 1] range.
   * @param categoryMask an {@link Optional} MPImage in IMAGE_FORMAT_ALPHA format representing a
   *     category mask, where each pixel represents the class which the pixel in the original image
   *     was predicted to belong to.
   * @param qualityScores The quality scores of the result masks, in the range of [0, 1]. Defaults
   *     to `1` if the model doesn't output quality scores. Each element corresponds to the score of
   *     the category in the model outputs.
   * @param timestampMs a timestamp for this result.
   */
  // TODO: consolidate output formats across platforms.
  public static ImageSegmenterResult create(
      Optional<List<MPImage>> confidenceMasks,
      Optional<MPImage> categoryMask,
      List<Float> qualityScores,
      long timestampMs) {
    return new AutoValue_ImageSegmenterResult(
        confidenceMasks.map(Collections::unmodifiableList),
        categoryMask,
        Collections.unmodifiableList(qualityScores),
        timestampMs);
  }

  public abstract Optional<List<MPImage>> confidenceMasks();

  public abstract Optional<MPImage> categoryMask();

  public abstract List<Float> qualityScores();

  @Override
  public abstract long timestampMs();
}
